function getMonthName(i) {
	switch (i) {
		case 12: return "December";
		case 11: return "November";
		case 10: return "October";
		case 9:  return "September";
		case 8:  return "August";
		case 7:  return "June";
		case 6:  return "July";
		case 5:  return "May";
		case 4:  return "April";
		case 3:  return "March";
		case 2:  return "Febuary";
		case 1:  return "January";
		default: return "";
	}
}


function renderDateSelector(parent) {

	var yearSelector = document.createElement("select");
	for (var i=2008; i>2002; i--) {
		yearSelector.options[yearSelector.options.length] = new Option(i, i);
	}
	parent.appendChild(yearSelector);

	var monthSelector = document.createElement("select");
	for (var i=12; i>0; i--) {
		monthSelector.options[monthSelector.options.length] = new Option(getMonthName(i), i);
	}
	parent.appendChild(monthSelector);

	var daySelector = document.createElement("select");
	for (var i=31; i>0; i--) {
		daySelector.options[daySelector.options.length] = new Option(i, i);
	}
	parent.appendChild(daySelector);

}




// ---------------------------------
// Primitive functions
// ---------------------------------
function addPrimitive(parent, removable, initValue) {
	var container = document.createElement("div");

	var input = document.createElement("input");
	input.type = "text";
	container.appendChild(input);

	if (initValue) {
		input.value = initValue;
	}

	parent.appendChild(container);

	if (removable) {
		renderRemove(container, removePrimitiveClick);
	}

	return container;
}
function addPrimitiveClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode;

	var typeContainer = addPrimitive(tdElm, true, null);
	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.addParamHandle(paramName, typeContainer);
}
function removePrimitiveClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode.parentNode;
	var typeContainer = elm.parentNode.parentNode;

	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.removeParamHandle(paramName, typeContainer);

	tdElm.removeChild(typeContainer);
}
function getPrimitiveString(container) {
	return container.firstChild.value;
}



// ---------------------------------
// Date Range functions
// ---------------------------------
function addDateRange(parent, removable, initValue) {
	var container = document.createElement("div");
	renderDateSelector(container);
	container.appendChild(document.createTextNode(" to "));
	renderDateSelector(container);
	parent.appendChild(container);

	if (initValue) {
		setDateRangeValue(container, initValue);
	}

	if (removable) {
		renderRemove(container, removeDateRangeClick);
	}

	return container;
}
function addDateRangeClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode;

	var typeContainer = addDateRange(tdElm, true, null);
	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.addParamHandle(paramName, typeContainer);
}
function removeDateRangeClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode.parentNode;
	var typeContainer = elm.parentNode.parentNode;

	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.removeParamHandle(paramName, typeContainer);

	tdElm.removeChild(typeContainer);
}
function getDateRangeString(container) {
	var fieldCount	= 0;
	var date1String = "";
	var date2String = "";
	for (var j=0; j<container.childNodes.length; j++) {
		if (container.childNodes[j].nodeName.toLowerCase() == "select") {
			var selector = container.childNodes[j];

			if (fieldCount < 3) {
				if (date1String != "")
					date1String += "-";
				date1String += selector.options[selector.options.selectedIndex].value;
			} else {
				if (date2String != "")
					date2String += "-";
				date2String += selector.options[selector.options.selectedIndex].value;
			}

			fieldCount++;
		}
	}

	return date1String+" to "+date2String;
}
function setDateRangeValue(container, datesString) {
	var dates = datesString.split("to");
	if (dates.length == 2) {
		var date1 = dates[0].trim().split("-");
		var date2 = dates[1].trim().split("-");

		if ( (date1.length == 3) && (date2.length == 3) ) {
			var y1 = date1[0];
			var m1 = date1[1];
			var d1 = date1[2];

			var y2 = date2[0];
			var m2 = date2[1];
			var d2 = date2[2];

			var fieldCount  = 0;
			for (var j=0; j<container.childNodes.length; j++) {
				if (container.childNodes[j].nodeName.toLowerCase() == "select") {
					var selector = container.childNodes[j];

					var val = -1;
					switch (fieldCount) {
						case 0:	val = y1; break;
						case 1:	val = m1; break;
						case 2:	val = d1; break;

						case 3:	val = y2; break;
						case 4:	val = m2; break;
						case 5:	val = d2; break;
					}

					selectOption(selector, val);

					fieldCount++;
				}
			}
		}
	}
}


// ---------------------------------
// Date functions
// ---------------------------------
function addDate(parent, removable, initValue) {
	var container = document.createElement("div");
	renderDateSelector(container);
	parent.appendChild(container);

	if (initValue) {
		setDateValue(container, initValue);
	}

	if (removable) {
		renderRemove(container, removeDateClick);
	}

	return container;
}
function addDateClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode;

	var typeContainer = addDate(tdElm, true, null);
	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.addParamHandle(paramName, typeContainer);

}
function removeDateClick(evt) {
	var elm = Event.findElement(evt);
	var tdElm = elm.parentNode.parentNode.parentNode;
	var typeContainer = elm.parentNode.parentNode;

	var paramName = tdElm.getAttribute("name");

	var rq = getReportQueryFromElement(elm);
	rq.removeParamHandle(paramName, typeContainer);

	tdElm.removeChild(typeContainer);
}
function getDateString(container) {
	var dateString = "";
	for (var j=0; j<container.childNodes.length; j++) {
		if (container.childNodes[j].nodeName.toLowerCase() == "select") {
			var selector = container.childNodes[j];

			if (dateString != "")
				dateString += "-";
			dateString += selector.options[selector.options.selectedIndex].value;
		}
	}

	return dateString;
}
function setDateValue(container, dateStr) {

	var date1 = dateStr.split("-");

	if (date1.length == 3) {
		var y1 = date1[0];
		var m1 = date1[1];
		var d1 = date1[2];

		var fieldCount  = 0;
		for (var j=0; j<container.childNodes.length; j++) {
			if (container.childNodes[j].nodeName.toLowerCase() == "select") {
				var selector = container.childNodes[j];

				var val = -1;
				switch (fieldCount) {
					case 0:	val = y1; break;
					case 1:	val = m1; break;
					case 2:	val = d1; break;
				}

				selectOption(selector, val);

				fieldCount++;
			}
		}
	}
}